package jmine.tec.web.servlet.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Filtro para desabilitar o cache de uma aplicacao web. Nao deve ser usada em produ��o.
 * 
 * @author takeshi
 */
public class DisableCacheFilter implements Filter {

    /**
     * 
     */
    public void destroy() {
    }

    /**
     * @param request ServletRequest
     * @param response ServletResponse
     * @param chain FilterChain
     * @throws IOException IOException
     * @throws ServletException Exception
     */
    public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException,
            ServletException {
        HttpServletResponse res = (HttpServletResponse) response;
        HttpServletRequest req = (HttpServletRequest) request;
        String uri = req.getRequestURI();
        if (!uri.endsWith(".gif") || !uri.endsWith(".jpg") || !uri.endsWith(".bmp")) { // pictures may be cached
            res.setHeader("cache-control", "no-cache");
            res.setHeader("Pragma", "no-cache");
        }
        chain.doFilter(request, response);
    }

    /**
     * @param filterConfig config
     * @throws ServletException ex
     */
    public void init(final FilterConfig filterConfig) throws ServletException {
    }

}
